package servlet;

import dao.DaoAlbum;
import dao.DaoArtista;
import dao.DaoMusica;
import entidades.Album;
import entidades.Artista;
import entidades.Musica;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tarde
 */
@WebServlet(name = "ServletGerenciamento", urlPatterns = {"/ServletGerenciamento"})
public class ServletGerenciamento extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String selecionar = request.getParameter("selecionar");
        String deletar = request.getParameter("deletar");
        String editar = request.getParameter("editar");
        String relacionar = request.getParameter("relacionar");
        if (selecionar != null) { //opção SELECIONAR
            HttpSession session = request.getSession();
            String[] str= selecionar.split("%"); //0= id, 1= tipo
            int id = Integer.parseInt(str[0]);
            if (str[1].equals("musica")) {
                Musica musica = (new DaoMusica()).get(id);
                session.setAttribute("tipo", "musica");
                session.setAttribute("selecionado", musica);
                
            }else if (str[1].equals("album")){
                Album album = new DaoAlbum().get(id);
                session.setAttribute("tipo", "album");
                session.setAttribute("selecionado", album);
                
            }else if (str[1].equals("artista")) {
                Artista artista = new DaoArtista().get(id);
                session.setAttribute("tipo", "artista");
                session.setAttribute("selecionado", artista);
            }
            
            session.setAttribute("selecionado", request);
        }else if (deletar != null) { //opção DELETAR
            String[] str = deletar.split("%");
            int id = Integer.parseInt(str[0]);
            if (str[1].equals("musica")) {
                new DaoMusica().remove(id);
            }else if (str[1].equals("album")) {
                new DaoAlbum().remove(id);
            }else if (str[1].equals("artista")) {
                new DaoArtista().remove(id);
            }
        }else if (editar != null) {//opção  EDITAR
            
        }else if (relacionar != null) {//opção RELACIONAR
            HttpSession session = request.getSession();
            String tipo1 = (String) session.getAttribute("tipo");
            String str = request.getParameter("relacionar");
            String tipo2  = str.split("%")[1];
            
            if (tipo1.equals(tipo2) || (tipo1.equals("musica") && tipo2.equals("artista")) || (tipo2.equals("musica") && tipo1.equals("artista")) )  {
                RequestDispatcher rd = request.getRequestDispatcher("gerenciamento.jsp");
                rd.forward(request, response);
            }
            if (tipo1.equals("musica")) {
                Musica m = (Musica) session.getAttribute("selecionado");
                int id = Integer.parseInt(str.split("%")[0]);
                Album a = new DaoAlbum().get(id);
                m.addAlbum(a);
                a.addMusica(m);
            }else if (tipo1.equals("album") && tipo2.equals("musica")) {
                Album a = (Album) session.getAttribute("selecionado");
                int id = Integer.parseInt(str.split("%")[0]);
                Musica m = new DaoMusica().get(id);
                a.addMusica(m);
                m.addAlbum(a);
            }else if (tipo1.equals("album") && tipo2.equals("artista")) {
                Album a = (Album) session.getAttribute("selecionado");
                int id = Integer.parseInt(str.split("%")[0]);
                Artista ar = new DaoArtista().get(id);
                a.addArtista(ar);
                ar.addAlbum(a);
            }else if (tipo1.equals("artista")) {
                Artista ar = (Artista) session.getAttribute("selecionado");
                int id = Integer.parseInt(str.split("%")[0]);
                Album a = new DaoAlbum().get(id);
                ar.addAlbum(a);
                a.addArtista(ar);
               
            }
         }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
